package com.mock.ems.controller.zone;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;
import com.mock.ems.common.JsonResult;

import java.util.*;

/**
 * Created by MOCK on 2017/3/22.
 */
public class ScoreController extends Controller {

    public void index() {


        Record record = Db.findFirst("select * from banner_config where type = 3 ");
        if (null != record) {
            record.set("banners",record.getStr("banners").split(","));
            record.set("urls",record.getStr("urls").split(","));
            setAttr("score",record.getColumns());
        }
        renderJsp("/page/zone/score/score_edit.jsp");
    }

    public void save() {
        //处理banners
        String banners = getPara("banners");
        String urls = getPara("bannerUrls");
        Record bannerConfig = Db.findFirst("select * from banner_config where type = 3 ");
        if (null == bannerConfig) {
            bannerConfig = new Record();
            bannerConfig.set("type",3)
                    .set("banners",banners)
                    .set("urls",urls);
            if (!Db.save("banner_config",bannerConfig)) {
                renderJson(new JsonResult().status("-1").show("保存失败"));
            }
        }else {
            bannerConfig.set("banners",banners)
                    .set("urls",urls);
            if (!Db.update("banner_config",bannerConfig)) {
                renderJson(new JsonResult().status("-1").show("保存失败"));
            }
        }
        renderJson(new JsonResult().status("000").show("保存成功"));
    }

    public void itemPagination(){

        int limit = getParaToInt("limit");
        int offset = getParaToInt("offset");
        int pageNumber = (offset / limit) + 1;

        String selectSql = "select a.*,b.partinNum,b.price";
        StringBuilder exceptSelectSQL = new StringBuilder(" from common_activity_partin a");
        exceptSelectSQL.append(" inner join common_activity_sku b on a.id = b.partinId where 1 = 1 and a.type = 3 ");

        List<Object> params = new ArrayList<Object>();

        Date beginTime = getParaToDate("beginTime");
        Date endTime = getParaToDate("endTime");

        if (null != beginTime && endTime == null) {
            exceptSelectSQL.append(" and a.partinTime >= ? ");
            params.add(beginTime);
        }

        if (null == beginTime && endTime != null) {
            exceptSelectSQL.append(" and a.partinTime <= ? ");
            params.add(endTime);
        }

        if (null != beginTime && null != endTime) {
            exceptSelectSQL.append(" and a.partinTime >= ? and a.partinTime <= ? ");
            params.add(beginTime);
            params.add(endTime);
        }

        Integer status = getParaToInt("status");
        if (status != null) {
            exceptSelectSQL.append(" and a.status =  ? ");
            params.add(status);
        }

        exceptSelectSQL.append(" order by a.id desc");

        Page<Record> page = Db.paginate(pageNumber,limit,selectSql,exceptSelectSQL.toString(),params.toArray());

        if(CollectionUtil.isNotEmpty(page.getList())){
            page.getList().stream().forEach(item->{
                Record shop = Db.findById("t_ou_shop","id_",item.getStr("shopId"));
                if(shop != null){
                    item.set("store_name_",shop.getStr("store_name_"));
                    item.set("contact_",shop.getStr("contact_"));
                }

                Record mallItem = Db.findById("t_prod_entity","id_",item.getStr("productId"));
                if(mallItem != null){
                    item.set("name_",mallItem.getStr("name_"));
                    item.set("sub_name_",mallItem.getStr("sub_name_"));
                }
            });
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", page.getTotalRow());
        returnMap.put("rows", page.getList());
        renderJson(returnMap);
    }

    public void audit() {

        String id_ = getPara("id");
        Record partin = Db.findById("common_activity_partin",id_);
        if(partin != null){
            String prodId = partin.getStr("productId");
            Record record = Db.findById("t_prod_entity","id_",prodId);

            record.set("id",id_);
            record.set("activityId",partin.getLong("activityId"));

            if (null != record) {
                String shopId = record.get("shop_id_");
                Record shopRecord = Db.findById("t_ou_shop","id_",shopId);
                if (null != shopRecord) {
                    String typeStr = shopRecord.get("business_type_");
                    shopRecord.set("business_type_","online_store".equals(typeStr) ? "线上店" : "oto".equals(typeStr) ? "线上实体店" : "未知类型");
                    record.set("shop",shopRecord.getColumns());
                }

                String mainImage = Db.queryStr("select cloud_path_ from t_prod_media where prod_id_ = ? and is_main_ = 'Y'",prodId);
                record.set("mainImage",mainImage);

                List<Record> subImageList = Db.find("select cloud_path_ from t_prod_media where prod_id_ = ? and is_main_ = 'N'",prodId);
                record.set("subImageList",subImageList);

                Record sku = Db.findFirst("select * from common_activity_sku where partinId = ?",id_);
                record.set("sku",sku);

                setAttr("record",record.getColumns());
            }
        }
        renderJsp("/page/zone/score/score_mallitem_audit.jsp");
    }
}
